﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;
using Fengsoft.AIBlog.Web.Models;
namespace Fengsoft.AIBlog.Web.Code
{
    public static class CacheHelper
    {
        public static List<BlobModel> Blobs
        {
            get
            {
                List<BlobModel> result=HttpContext.Current.Cache["Blobs"] as List<BlobModel>;
                if (result==null)
                {
                    result=ResetBlobs();
                }
                return result;
            }
        }
        public static string[] IdolNames
        {
            get
            {
                return Blobs.Select(x => x.Name).Distinct().ToArray();
            }
        }
        public static List<BlobModel> ResetBlobs()
        {
            string blobPath=HostingEnvironment.MapPath("~/app_data/blob");
            string[] fileNames=Directory.GetFiles(blobPath);
            List<BlobModel> result=new List<BlobModel>(fileNames.Length);
            foreach(string fileName in fileNames)
            {
                result.Add(new BlobModel(fileName));
            }
            HttpContext.Current.Cache.Insert("Blobs",result);
            return result;
        }
        public static List<string> Themes
        {
            get
            {
                List<string> result=HttpContext.Current.Cache["Themes"] as List<string>;
                if (result==null)
                {
                    string themePath=HttpContext.Current.Server.MapPath("~/assets/css/themes");
                    result=new List<string>();
                    foreach (string dir in Directory.GetDirectories(themePath))
                    {
                        DirectoryInfo dirInfo=new DirectoryInfo(dir);
                        result.Add(dirInfo.Name);
                    }
                    HttpContext.Current.Cache.Insert("Themes",result);
                }
                return result;
            }
        }
    }
}